var app = getApp();
var util = require('../../../utils/util.js');
import {date,years,months,days,today} from '../../../utils/date';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    chose_one_class:{},  //当前选择分类索引表
    win_chose_one_class:{},  //当前选择分类索引表
    is_screen:false,  //分类筛选
    isinput_col:false,  //收藏弹窗显隐
    left_open: 0,    //默认打开第一个供应商索引
    isInput: false,  //添加商品数量弹框及输入框焦点
    inputNum: "",   //添加商品弹窗的输入框中数量
    count: 0, //选中数量
    merge_ids: [],  //小单id列表 
    shop_ids: [],  //购物车列表
    isChooseAll: false, //全选
    is_checked: {},//单选
    isclasscheck:{}, //类选
    tanimg: "",   //弹窗商品图片
    tanname: "",  //弹窗商品名称
    tanunit: "",  //弹窗商品单位
    tangui: "",   //弹窗规格名称
    tannum: "",   //弹窗商品数量
    tszhinput: "",     //备注输入框内容
    tszhcontent: "",   //备注内容
    KeyboardKeys: [1, 2, 3, 4, 5, 6, 7, 8, 9, '·', 0], //键盘
    searcontent: "",   //搜索内容
    issear: false,   //是否搜索
    page: 1,    //搜索初始页码
    searlists: [],  //搜索列表
    seartop_z: 0,
    content: '',  //键盘初始空字符串
    one_super:0,
    tan_shop_ku:0,  //当前商品是否属于库商品
    group_name:'',   //所加入的集团名称
    tan_supername:'',  //当前菜品的供应商
    assign_super_index:-1,  //选择的供应商
    tan_is_auxiliary:0,  //当前商品是否开启辅助单位  0关闭，1开启
    aux_kucun_index:-1,   //库存商品辅助单位索引
    assign_superlist:[],
    aux_pf_list:[], //gy_pf辅助单位列表
    aux_pf_index:-1, //gy_pf辅助单位索引
    is_move_gif:false,  //按钮动图
    today:today,//今天
    isNowArea:false,  //当前是否在规定下单时间中
    is_time_on:-1,  //企业是否打开了下单时间设置  -1默认，0关，1开
    shop_count:-1,  //-1初始 购物车中的商品数
    car_total_money:0  //购物车商品价格合计
  },
  // 回到商品详情页
  shopTab: function () {
    wx.navigateBack({
      delta:1
    });
  },
  // 打开特殊需求备注弹窗
  opentszh: function () {
    this.setData({
      isInput2: true,
      istszh: true,
      tszhinput: ""
    })
  },
  // 特殊需求备注弹窗输入内容
  tszhinput: function (e) {
    this.setData({
      tszhinput: e.detail.value
    })
  },
  // 关闭特殊需求备注弹窗
  tszhcancel: function () {
    this.setData({
      isInput2: false,
      istszh: false
    })
  },
  // 确定并关闭需求备注弹窗
  tszhconfirm: function () {
    this.setData({
      isInput2: false,
      istszh: false,
      tszhcontent: this.data.tszhinput
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const pages = getCurrentPages();    //获取当前页面信息栈
    app.globalData.prevPage = pages[pages.length - 2].route     //获取上一个页面信息  
    var that = this;
    that.setData({
      is_zi_cai:app.globalData.loginInfo.is_zi_cai,  //是否自采
      good_dir_set:app.globalData.loginInfo.good_dir_set,    //目录类型
      client_is_store:app.globalData.loginInfo.client_is_store,  //<0未加入集团，==0加入集团无库 ,==1加入集团有库
      purchase_type:app.globalData.loginInfo.purchase_type,  //1门店采购，2总部采购
      is_ctrl_stock:app.globalData.loginInfo.is_ctrl_stock,  //库存控制开启（1开，0关），1库存不足则不可下单
      group_id:app.globalData.loginInfo.group_id,
      group_name:app.globalData.loginInfo.group_name,  //集团名称
      price_type:options.price_type,   //客户定价模式
      show_price_type:options.show_price_type,
      is_ku:options.is_ku,
      two_specs_h: 0.6*wx.getSystemInfoSync().windowHeight,
      loading:1,
      is_sao_code:options.is_sao_code,
      sao_code:options.sao_code
    });
    if (options.supplier_id && options.supplier_id>0 || options.supplier_id==-1) {
      that.setData({
        supplier_id: options.supplier_id,
        b_supplier_id: 0,   //b_supplier_id传值
        one_super:1,
        tan_supername:options.supplier_id==-1?'':options.supername
      });
      wx.setNavigationBarTitle({
        title: options.supername+"-购物车"
      });
      that.updateGouWuChe(options.supplier_id,1);
    } else {
      that.setData({
        supplier_id: 0, //供应商初始id
        one_super: 0
      });
      wx.setNavigationBarTitle({
        title: "购物车"
      });
      that.updateGouWuChe(0,0);
    }
    if(options.is_sao_code==1){
      that.setData({
        searcontent:options.sao_code
      });
      that.viewck(options.is_sao_code);
    }
  },
  noneEnoughPeople(){
    return;
  },
  // 扫描商品条码
  saomiao_ck(){
    let that = this;
    wx.scanCode({
      onlyFromCamera:true, 
      success: (res) => { //扫码成功后
          console.log(res)
          wx.showToast({
            title: '扫码成功',
            duration: 1000,
            mask: true
          });
          that.setData({
            is_sao_code:1,
            searcontent:res.result
          });
          that.viewck(1);
        },
        fail: (res) => {//扫码失败后
          wx.showToast({
             title: '扫码失败',
             icon: 'loading',
             duration: 1500
          });
          that.setData({
            is_sao_code:0
          });
        },
      complete:(res)=>{
        console.log(res);
      }
    })
  },
  // 购物车列表刷新
  updateGouWuChe: function (supplier_id, one_super) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    console.log(supplier_id);
    app.AppHttp('api/Shopping/getShoppingSmInfo', {
      openid: app.globalData.openid,
      supplier_id: supplier_id,
      is_ku:that.data.is_ku
    }, function (res) {
      console.log(JSON.stringify(res.data));
      if (res.data.status==-1){
        that.setData({
          leftTypes:[],
          shop_count:0,
          car_total_money:0,
          rightDatas:[]
        });
        wx.hideLoading();
        return;
      };
      var supplier_list_left = res.data.supplier_list_left;
      if (one_super==1 && supplier_id!=-1){
        supplier_list_left=[];
        if (res.data.count!=0){
          for (var i = 0; i < res.data.supplier_list_left.length; i++) {
            if (res.data.supplier_list_left[i].id == supplier_id) {
              supplier_list_left.push(res.data.supplier_list_left[i]);
            }
          }
        }
        that.setData({
          status:1
        });
      };
      let rightDatas={};
      let shop_count=0;
      let car_total_money = 0;
      console.log('that.data.price_type'+that.data.price_type);
      Object.keys(res.data.result).forEach(function(one_class){
        if(JSON.stringify(that.data.chose_one_class) == "{}" || that.data.chose_one_class[one_class]==true){
          rightDatas[one_class]=res.data.result[one_class];
          shop_count+=res.data.result[one_class].length;
          res.data.result[one_class].forEach(function(v){
            if(that.data.price_type==1 && that.data.good_dir_set<=1){
              car_total_money += parseFloat(v.history_price)*(v.pr_num*1);
            }else{
              car_total_money += parseFloat(v.pr_price_b)*(v.pr_num*1);
            };
          });
        }
      });
      that.setData({
        supplier_id: res.data.supplier_id==0?-1:res.data.supplier_id,
        leftTypes: supplier_list_left,
        rightDatas: rightDatas,
        shop_count:shop_count,
        car_total_money:car_total_money.toFixed(2),
        chose_one_class_count:Object.keys(that.data.chose_one_class).length,
        status:supplier_list_left.length==0?"":supplier_list_left[that.data.left_open].status,  //1正常 0暂时合作 -1待审核
        order_time_range:res.data.order_time_range,
        order_time_range2:res.data.order_time_range2,
        client_status:res.data.client_status,
        is_time_on:res.data.is_time_on,
        car_is_show_price:res.data.is_show_price,  //是否显示固定价
        loading:0
      });
      //当前是否在下单时间范围
      var val_arr=(res.data.order_time_range).split(' - ');
      var start_time=(today+' '+val_arr[0]).replace(/-/g,'/');
      var end_time=(today+' '+val_arr[1]).replace(/-/g,'/');
      start_time=Date.parse(new Date(start_time));
      end_time=Date.parse(new Date(end_time));
      var now_time=Date.parse(new Date());
      if(start_time<end_time && now_time>=start_time && now_time<=end_time || start_time>end_time &&(now_time>=start_time || now_time<=end_time)){
        that.setData({
          isNowArea:true
        });
      }else{
        var isNowArea;
        if(res.data.order_time_range2){
          var val_arr2=(res.data.order_time_range2).split(' - ');
          var start_time2=(today+' '+val_arr2[0]).replace(/-/g,'/');
          var end_time2=(today+' '+val_arr2[1]).replace(/-/g,'/');
          start_time2=Date.parse(new Date(start_time2));
          end_time2=Date.parse(new Date(end_time2));
          if(start_time2<end_time2 && now_time>=start_time2 && now_time<=end_time2 || start_time2>end_time2 &&(now_time>=start_time2 || now_time<=end_time2)){
            isNowArea=true;
          }else{
            isNowArea=false;
          };
        }else{
          isNowArea=false;
        };
        that.setData({
          isNowArea:isNowArea
        });
      };
      wx.hideLoading();
    })
  },
  //供应商未报价
  zero_price_but:function(){
    wx.showLoading({
      title:'供应商未报价',
      duration:500
    })
  },
  // 修改商品数量弹窗
  numInput: function (e) {  
    util.showBuyModal(this);
    var val = e.currentTarget.dataset.val;
    console.log(val);
    this.setData({
      isInput: true,
      is_addshop:false,   //修改商品数量
      tanid: val.id,
      tanimg: val.cover,
      tanname: val.pr_name,
      tanspecs:val.specs,
      tanunit: val.pr_unit_name,
      tanunit_init: val.original_good_unit_name,
      tannum: val.pr_num,
      tan_supername:val.supplier_name,
      tan_superprice:val.pr_price_b,
      tan_supplier_id:val.supplier_id,
      tan_b_supplier_id:val.b_supplier_id,
      tan_shop_ku:val.b_supplier_id==this.data.group_id?1:0,
      tan_ku_num:val.stock_num_show,
      // tangui: val.specs,
      tszhcontent: val.remark?val.remark:""
    });
  },
  // 输入数量
  setValue: function () {
    this.setData({
      inputNum: this.data.content
    });
  },
  // 关闭修改数量商品弹窗
  cancel: function () {
    util.hideBuyModal(this,"isInput");
    this.setData({
      inputIndex:-1,
      inputNum: "",
      tannum:'',
      shopnum:'',
      shoppr_num:'',
      tszhcontent: '',
      content: ""
    });
  },
  confirm: function () {
    if (this.data.inputNum == '') {
      this.setData({
        inputNum: this.data.tannum
      });
    }
    if (this.data.inputNum > 9999.99) {
      wx.showToast({
        title: '数量过大',
      })
      return;
    }
    this.data.tannum = this.data.inputNum * 1;
    if(this.data.tan_shop_ku==1 && this.data.is_ctrl_stock==1 && this.data.tannum>0 && this.data.tannum*1>this.data.tan_ku_num*1){
      wx.showLoading({
        title:'库存不足',
        duration:500
      });
      return;
    };
    util.hideBuyModal(this,"isInput");
    this.setData({
      inputNum: "",
      content: ""
    });
    console.log(this.data.tannum);
    app.myMethod.updateGoodNum(this.data.tanid, this.data.tannum, this.data.tszhcontent);
    this.setData({
      tszhcontent: ''
    })
    this.updateGouWuChe(this.data.supplier_id, this.data.one_super);
  },
  // 单选
  checkboxMerge(e){
    let zindex = e.currentTarget.dataset.zindex;
    let id = e.currentTarget.dataset.id;
    let is_checked = this.data.is_checked;
    let isclasscheck = this.data.isclasscheck; //类选是否选中
    let rightDatas = this.data.rightDatas;
    if(is_checked[id]){
      delete is_checked[id];
    }else{
      is_checked[id]=true;
    };
    let class_result=rightDatas[zindex].every(function(v){
      return is_checked[v.id];
    });
    isclasscheck[zindex] = class_result;
    this.setData({
      isclasscheck:isclasscheck,
      is_checked:is_checked,
      count:Object.keys(is_checked).length,
      isChooseAll:Object.keys(is_checked).length==this.data.shop_count?true:false
    });
  },
  // 类选
  checkboxclass(e){
    let zindex = e.currentTarget.dataset.zindex;
    let is_checked = this.data.is_checked;
    let isclasscheck = this.data.isclasscheck; //类选是否选中
    let rightDatas = this.data.rightDatas;
    if(isclasscheck[zindex]){
      delete isclasscheck[zindex];
      rightDatas[zindex].forEach(function(v){
        if(is_checked[v.id]){
          delete is_checked[v.id];
        }
      });
    }else{
      isclasscheck[zindex]=true;
      rightDatas[zindex].forEach(function(v){
        is_checked[v.id] = true;
      });
    };
    this.setData({
      isclasscheck:isclasscheck,
      is_checked:is_checked,
      count:Object.keys(is_checked).length,
      isChooseAll:Object.keys(is_checked).length==this.data.shop_count?true:false
    });
  },
  // 全选
  chooseAll() {
    let isChooseAll = !this.data.isChooseAll;
    let is_checked = this.data.is_checked;
    let isclasscheck = this.data.isclasscheck; //类选是否选中
    let rightDatas = this.data.rightDatas;
    if(isChooseAll) {
      Object.keys(rightDatas).forEach(function(zindex){
        isclasscheck[zindex]=true;
        rightDatas[zindex].forEach(function(v){
          is_checked[v.id] = true;
        });
      });
    }else{
      is_checked = {};
      isclasscheck = {};
    };
    this.setData({
      isclasscheck:isclasscheck,
      is_checked:is_checked,
      isChooseAll: isChooseAll,
      count: isChooseAll == true ? this.data.shop_count : 0
    })
  },
  // 删除/下单
  submit_but(e){
    let type = e.currentTarget.dataset.type;
    let is_checked_nocanku = {};
    let rightDatas = this.data.rightDatas;
    let isclasscheck = this.data.isclasscheck;
    let is_checked = this.data.is_checked;
    let that = this;
    if(this.data.group_id>0 && type == 'sub'){   //属于企业下单时的限制因素
      if(this.data.client_status==0){  //0表示您已被暂停，1则正常
        wx.showModal({
          title: '提示',
          content:'您已被企业暂停，暂时不能下单',
          showCancel: false,
          confirmText:"我知道了",
          success: function (res) {}
        });    
        return;
      }else if(this.data.is_time_on==1 && !this.data.isNowArea){  //企业规定的时间
        wx.showModal({
          title: '提示',
          content:'当前时间已超出企业的下单时间',
          showCancel: false,
          confirmText:"我知道了",
          success: function (res) {}
        });    
        return;
      };
    };
    Object.keys(rightDatas).forEach(function(zindex){
      rightDatas[zindex].forEach(function(v){
        if(v.b_supplier_id==that.data.group_id && that.data.is_ctrl_stock==1 && v.stock_num_show*1<v.pr_num*1){
          is_checked_nocanku[zindex+'#'+v.id] = true;
        };
      });
    });
    let shopIdS = Object.keys(is_checked);
    if (shopIdS.length == 0) {
      wx.showLoading({
        title: '请至少选择一个订单',
        duration: 500
      });
    }else{
      if(type == 'sub'){ //下单
        if(this.data.is_ctrl_stock==1 && Object.keys(is_checked_nocanku).length>0){
          wx.showModal({
            title: '提示',
            content:'企业已开启库存控制功能\n缺货或余量小于预定量的商品\n不可下单',
            showCancel: false,
            confirmText:"我知道了",
            success: function (res) {
              for(var y in is_checked_nocanku){
                let d=y.split('#');
                delete is_checked[d[1]];
                delete isclasscheck[d[0]];
              }
              that.setData({
                isChooseAll:false,
                count:Object.keys(is_checked).length,
                is_checked:is_checked,
                isclasscheck:isclasscheck
              });
            }
          });
          return;
        };
        wx.navigateTo({
          url: '/pages/dingdan/dingdantianxie/dingdantianxie?shop_ids='+ shopIdS +'&type='+'shopcar'+'&is_ku='+that.data.is_ku+'&price_type='+this.data.price_type
        });
      }else if(type == 'del'){ //删除
        this.delClick(shopIdS);
      };
    }
  },
  // 删除商品
  delClick: function (shopidS) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '请确认删除',
      success: function (res) {
        if (res.confirm) {
          app.AppHttp('api/Shopping/batchDelShopping', {
            ids: shopidS,
            openid: app.globalData.openid
          }, function (res) {
            app.globalData.changyong_show = 1;
            if (that.data.isChooseAll == true) { //全选时，供应商id恢复默认，
              that.setData({
                left_open: 0,
              });
              if (that.data.one_super == 0){  //恢复默认第一个供应商索引
                that.setData({
                  supplier_id:0
                })
              };
            };
            that.setData({
              count: 0,    //删除后已选商品数量归零
              isChooseAll: false,    //全选
              isclasscheck:{},  //类选
              is_checked:{},      //单选
            })
            that.updateGouWuChe(that.data.supplier_id, that.data.one_super);
          })
        }
      }
    })
  },
  // 输入搜索内容
  searinput: function (e) {
    var that = this;
    if (e.detail.value != that.data.searcontent) {
      that.setData({
        searlists: [],
        searcontent: e.detail.value,
        page: 1,
        load: '搜索中',
        seartop: 0
      });
      app.globalData.task.abort();  //中断上一次搜索
      that.viewck();
    }
  },
  searchClick: function () {
    app.globalData.task.abort();  //中断上一次搜索
    this.viewck();
  },
  // 搜索获得焦点
  searfocus: function () {
    if (this.data.searcontent != '') {
      this.setData({
        issear: true,
        seartop: this.data.seartop_z
      });
    }
  },
  // 清除搜索
  clear_sear: function () {
    var that = this;
    setTimeout(function (){
      that.setData({
        searlists: [],
        searcontent: '',
        page: 1,
        load: "搜索中"
      })
    },100)
  },
  // 搜索接口
  viewck: function (is_sao_code) {
    var that = this;
    wx.showLoading({
      title: "搜索中..."
    });
    app.AppHttp('api/Goods/SearchGood', {
      keyword: that.data.searcontent,
      supplier_id: that.data.supplier_id,
      openid: app.globalData.openid,
      page: that.data.page,
      limit:20,
      is_ku:that.data.is_ku
    }, function (res) {
      console.log(JSON.stringify(res));
      if(is_sao_code==1){
        if(res.data[0].length==0){
          wx.showModal({
            title: '提示',
            content:'没有搜到结果',
            showCancel: false,
            confirmText:"我知道了",
            success: function (res) {}
          });   
          return;
        }else if(res.data[0].length==1){
          that.setData({
            searlists: res.data[0],
          });
          that.sao_only_one(res.data[0][0]);
        }else{
          if (that.data.page == 1) {
            that.setData({
              searlists: res.data[0],
              allpage: res.data[1],
              issear: true
            });
          } else {
            that.setData({
              searlists: that.data.searlists.concat(res.data[0]),
              allpage: res.data[1],
              issear: true
            });
          };
        };
      }else{
        if (that.data.page == 1) {
          that.setData({
            searlists: res.data[0],
            allpage: res.data[1],
            issear: true
          });
        } else {
          that.setData({
            searlists: that.data.searlists.concat(res.data[0]),
            allpage: res.data[1],
            issear: true
          });
        };
      };
      wx.hideLoading()
    });
  },
  // 条码扫描结果仅有一条
  sao_only_one(item){
    if(item.specs_count>0 || item.price_type>=3 && this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && (item.is_auxiliary==0 && (item.is_up*1+this.data.is_zi_cai*1)>1 && item.is_ku==0 || item.is_auxiliary==1 && ((item.is_aux_up*1+this.data.is_zi_cai*1)>1 && item.is_ku==0 || item.is_aux_up*1>1 && item.is_ku==1)) || item.is_auxiliary==1 && item.price_type<=2 && (this.data.price_type<=2 || this.data.price_type==4) && item.goodAuxCount*1>1){
      var index =0;
      var is_up=item.is_up;  //是否下架及供应商数量
      var is_ku=item.is_ku;  //当前商品是否属于库存商品
      var is_zi_cai=this.data.is_zi_cai;  //是否自采
      var specs_count=item.specs_count;  //是否多规格
      var tan_is_auxiliary=item.is_auxiliary;  //是否开启辅助单位
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){
        if(is_up*1+is_zi_cai*1==0 && is_ku==0){
          wx.showModal({
            title: '提示',
            content:'没有搜到结果',
            showCancel: false,
            confirmText:"我知道了",
            success: function (res) {}
          });
          return;
        };
        if(is_ku==1){   //库存商品
          this.setData({
            is_move_gif:tan_is_auxiliary==1?true:false,
            tan_shop_ku:1,
            aux_kucun_index:-1,
            aux_kucun_list:[],
            tan_is_auxiliary:tan_is_auxiliary
          });
          if(tan_is_auxiliary==1){
            this.aux_kucun_fun(item.id);
          };
        }else if(is_ku==0){  //非库存商品
          this.setData({
            tan_shop_ku:0,
            tan_is_auxiliary:tan_is_auxiliary,
            assign_superlist:[],
            is_move_gif:true
          });
          if(specs_count==0){  //仅有主规格
            this.assign_super_fun(item.id,tan_is_auxiliary);
          }; 
        };
      }else{
        if(this.data.searlists[index].price_type<=2){  //供应-批发
          this.setData({
            aux_pf_index:-1,
            aux_pf_list:[],
            is_move_gif:tan_is_auxiliary==1?true:false,
            tan_is_auxiliary:tan_is_auxiliary,
          })
          if(tan_is_auxiliary==1 && specs_count==0){  //仅有主规格
            this.aux_gy_pf_fun(item.id);
          }; 
        }else{  //其他模式的企业
          this.setData({
            tan_is_auxiliary:tan_is_auxiliary,
            is_move_gif:false
          });
        }
      };
      this.setData({
        assign_super_index:-1,
        shopid:item.id,
        issear: false,
        inputIndex: index,
        shop_is_same:this.data.searlists[index].is_same,
        ischoose_index: -1,
        tszhcontent: ''
      });
      this.tancontent(); 
    }else{
      var index = 0;
      var info = this.data.searlists[index];
      var is_up=item.is_up;   //是否下架
      var is_ku=item.is_ku;   //是否属于库存商品
      var is_zi_cai=this.data.is_zi_cai;   //客户是否自采
      var tan_is_auxiliary=item.is_auxiliary;  //是否开启辅助单位
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){  //模式5
        if(is_up*1+is_zi_cai*1==0 && is_ku==0){
          wx.showModal({
            title: '提示',
            content:'没有搜到结果',
            showCancel: false,
            confirmText:"我知道了",
            success: function (res) {}
          }); 
          return;
        };
        if(is_ku==0){
          this.setData({
            tan_shop_ku:0,  //当前商品不属于库商品
            assign_superlist:[]
          });
          if(is_zi_cai==0){  //非自采
            this.setData({
              is_move_gif:true,
              tan_is_auxiliary:tan_is_auxiliary,
              assign_super_index:0,
              aux_kucun_index:-1,
              aux_kucun_list:[],
            });
            this.assign_super_fun(info.id,tan_is_auxiliary);
          }else if(is_zi_cai==1){  //自采
            this.setData({
              assign_super_index:-10010,
              aux_kucun_index:-1,
              aux_kucun_list:[],
              is_move_gif:false,
              tan_is_auxiliary:tan_is_auxiliary
            });
          };
        }else if(is_ku==1){
          if(this.data.is_ctrl_stock==1 && info.stock_num_show<=0){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          this.setData({
            tan_is_auxiliary:tan_is_auxiliary,
            assign_super_index:-1,
            aux_kucun_index:tan_is_auxiliary==1?0:-1,
            is_move_gif:tan_is_auxiliary==1?true:false,
            tan_shop_ku:1  //当前商品属于库商品
          });
          if(tan_is_auxiliary==1){
            this.aux_kucun_fun(info.id);
          };
        };  
      }else{
        if(info.price_type<=2){ //供应-批发
          this.setData({
            aux_pf_list:[],
            tan_is_auxiliary:tan_is_auxiliary
          })
          if(tan_is_auxiliary==0){
            this.setData({
              aux_pf_index:-1,
              is_move_gif:false
            })
          }else if(tan_is_auxiliary==1){ //辅助单位
            this.setData({
              aux_pf_index:0,
              is_move_gif:true
            })
            this.aux_gy_pf_fun(info.id);
          };
        }else{
          this.setData({
            tan_is_auxiliary:tan_is_auxiliary,
            assign_super_index:-1,
            is_move_gif:false
          });
        }
      }; 
      util.showBuyModal(this);
      this.setData({
        issear:false,
        content:'',
        tszhcontent:'',
        isInput: true,
        is_addshop:true,   //添加商品
        inputIndex: index,
        inputNum: '',
        tanimg: info.cover,
        tanname: info.trade_name,
        tanalias:info.trade_alias,
        tanspecs:info.specs,
        tanunit_index: info.help_unit>0?info.help_unit:info.unit,
        tanunit: info.help_unit>0?info.help_unit_name:info.unit_name,
        tanunit_init:info.unit_name,
        tan_price_type:info.price_type,
        tan_true_price:info.true_price,
        tan_single_price:this.data.show_price_type=="B"?info.level_single_price:info.level_true_price,  //级别价
        tan_sale_price:info.sale_price,
        ischoose_index: 0, 
        tszhcontent:'', 
        shopid:info.id,
        shoppr_num:info.pr_num,  //选中的产品是否已添加
        shopnum:'',
        shop_price_type:info.price_type,  //选中的产品价格类型
        shop_price:info.true_price, //选中的产品的价格
        shop_b_price:info.single_price, //选中的产品B单价格
        shop_sale_price:info.sale_price, //选中的产品销售价
        shop_level_price:info.level_true_price,  //级别A价
        shop_level_b_price:info.level_single_price,  //级别B价
        shop_is_same:info.is_same,  //是否开启了采销同价
        tan_ku_price:info.is_same==0?info.stock_sale_price:info.stock_price_show,
        tan_ku_num:info.stock_num_show,
        is_tan_ku_price:info.is_show_stock_price,
        is_tan_ku_num:info.is_show_stock_num
      });      
    };
  },
  // 正在滚动
  searing: function (e) {
    this.setData({
      seartop_z: e.detail.scrollTop  //滚动距顶部的距离
    });
  },
  // 搜索滚动到底部
  searscroll: function (e) {
    var that = this;
    console.log(that.data.page);
    if (that.data.page < that.data.allpage) {
      that.setData({
        page: that.data.page + 1,
        load: '加载更多'
      });
      that.viewck();
    } else {
      wx.showToast({
        title: '没有了'
      })
    }
  },
  // 关闭搜索内容
  closesear: function () {
    this.setData({
      issear: false
    })
  },
  /// 选择的搜索内容
  searchose: function (e) {
    var index = e.currentTarget.id;
    var is_up=e.currentTarget.dataset.is_up;  //是否下架及供应商数量
    var is_ku=e.currentTarget.dataset.is_ku;  //当前商品是否属于库存商品
    var is_zi_cai=this.data.is_zi_cai;  //是否自采
    var specs_count=e.currentTarget.dataset.specs_count;  //是否多规格
    var tan_is_auxiliary=e.currentTarget.dataset.is_auxiliary;  //是否开启辅助单位
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){
      if(is_up*1+is_zi_cai*1==0 && is_ku==0){
        return;
      };
      if(is_ku==1){   //库存商品
        this.setData({
          is_move_gif:tan_is_auxiliary==1?true:false,
          tan_shop_ku:1,
          aux_kucun_index:-1,
          aux_kucun_list:[],
          tan_is_auxiliary:tan_is_auxiliary
        });
        if(tan_is_auxiliary==1){
          this.aux_kucun_fun(e.currentTarget.dataset.shopid);
        };
      }else if(is_ku==0){  //非库存商品
        this.setData({
          tan_shop_ku:0,
          tan_is_auxiliary:tan_is_auxiliary,
          assign_superlist:[],
          is_move_gif:true
        });
        if(specs_count==0){  //仅有主规格
          this.assign_super_fun(e.currentTarget.dataset.shopid,tan_is_auxiliary);
        }; 
      };
    }else{
      if(this.data.searlists[index].price_type<=2){  //供应-批发
        this.setData({
          aux_pf_index:-1,
          aux_pf_list:[],
          is_move_gif:tan_is_auxiliary==1?true:false,
          tan_is_auxiliary:tan_is_auxiliary,
        })
        if(tan_is_auxiliary==1 && specs_count==0){  //仅有主规格
          this.aux_gy_pf_fun(e.currentTarget.dataset.shopid);
        }; 
      }else{  //其他模式的企业
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          is_move_gif:false
        });
      }
    };
    this.setData({
      assign_super_index:-1,
      shopid:e.currentTarget.dataset.shopid,
      issear: false,
      inputIndex: index,
      shop_is_same:this.data.searlists[index].is_same,
      ischoose_index: -1,
      tszhcontent: ''
    });
    this.tancontent(); 
  },
  itemAddClick: function (e) {
    var index = e.currentTarget.id;
    var info = this.data.searlists[index];
    var is_up=e.currentTarget.dataset.is_up;   //是否下架
    var is_ku=e.currentTarget.dataset.is_ku;   //是否属于库存商品
    var is_zi_cai=this.data.is_zi_cai;   //客户是否自采
    var tan_is_auxiliary=e.currentTarget.dataset.is_auxiliary;  //是否开启辅助单位
    if(this.data.good_dir_set==1){  
      tan_is_auxiliary=0; //供应商目录 的 相关辅助单位（等企业相关辅助单位完善后可做），目前假定没有辅助单位
    };
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){  //模式5
      if(is_up*1+is_zi_cai*1==0 && is_ku==0){
        return;
      };
      if(is_ku==0){
        this.setData({
          tan_shop_ku:0,  //当前商品不属于库商品
          assign_superlist:[]
        });
        if(is_zi_cai==0){  //非自采
          this.setData({
            is_move_gif:true,
            tan_is_auxiliary:tan_is_auxiliary,
            assign_super_index:0,
            aux_kucun_index:-1,
            aux_kucun_list:[],
          });
          this.assign_super_fun(info.id,tan_is_auxiliary);
        }else if(is_zi_cai==1){  //自采
          this.setData({
            assign_super_index:-10010,
            aux_kucun_index:-1,
            aux_kucun_list:[],
            is_move_gif:false,
            tan_is_auxiliary:tan_is_auxiliary
          });
        };
      }else if(is_ku==1){
        if(this.data.is_ctrl_stock==1 && info.stock_num_show<=0){
          wx.showLoading({
            title:'库存不足',
            duration:500
          });
          return;
        };
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          assign_super_index:-1,
          aux_kucun_index:tan_is_auxiliary==1?0:-1,
          is_move_gif:tan_is_auxiliary==1?true:false,
          tan_shop_ku:1  //当前商品属于库商品
        });
        if(tan_is_auxiliary==1){
          this.aux_kucun_fun(info.id);
        };
      };  
    }else{
      if(info.price_type<=2){ //供应-批发
        this.setData({
          aux_pf_list:[],
          tan_is_auxiliary:tan_is_auxiliary
        })
        if(tan_is_auxiliary==0){
          this.setData({
            aux_pf_index:-1,
            is_move_gif:false
          })
        }else if(tan_is_auxiliary==1){ //辅助单位
          this.setData({
            aux_pf_index:0,
            is_move_gif:true
          })
          this.aux_gy_pf_fun(info.id);
        };
      }else{
        this.setData({
          tan_is_auxiliary:tan_is_auxiliary,
          assign_super_index:-1,
          is_move_gif:false
        });
      }
    }; 
    util.showBuyModal(this);
    let tan_true_price='';
    let tan_single_price='';
    if(this.data.price_type==2){
      tan_true_price=info.level_true_price;
      tan_single_price=info.level_single_price;
    }else{
      tan_true_price=info.true_price;
      tan_single_price=info.single_price;
    };
    this.setData({
      issear:false,
      content:'',
      tszhcontent:'',
      isInput: true,
      is_addshop:true,   //添加商品
      inputIndex: index,
      inputNum: '',
      tanimg: info.cover,
      tanname: info.trade_name,
      tanalias:info.trade_alias,
      tanspecs:info.specs,
      tan_fixed_show:info.is_show_price,  //是否显示固定价
      tanunit_index: info.help_unit>0?info.help_unit:info.unit,
      tanunit: info.help_unit>0?info.help_unit_name:info.unit_name,
      tanunit_init:info.unit_name,
      tan_price_type:info.price_type,
      tan_true_price:tan_true_price,
      tan_single_price:tan_single_price,
      tan_sale_price:info.sale_price,
      ischoose_index: 0, 
      tszhcontent:'', 
      shopid:info.id,
      shoppr_num:info.pr_num,  //选中的产品是否已添加
      shopnum:'',
      shop_price_type:info.price_type,  //选中的产品价格类型
      shop_price:info.true_price, //选中的产品的价格
      shop_b_price:info.single_price, //选中的产品B单价格
      shop_sale_price:info.sale_price, //选中的产品销售价
      shop_level_price:info.level_true_price,  //级别A价
      shop_level_b_price:info.level_single_price,  //级别B价
      shop_is_same:info.is_same,  //是否开启了采销同价
      tan_ku_price:info.is_same==0?info.stock_sale_price:info.stock_price_show,
      tan_ku_num:info.stock_num_show,
      is_tan_ku_price:info.is_show_stock_price,
      is_tan_ku_num:info.is_show_stock_num
    }); 
  },
  add_confirm() {
    var that = this;
    var shopnum = that.data.shopnum;
    if (that.data.ischoose_index == -1) {
      wx.showLoading({
        title: '请选择规格',
        duration: 500
      });
      return;
    }else if(that.data.shopnum==0){
      if(that.data.shopnum != ''){ //输入0
        shopnum=0;
      }else if(that.data.shopnum == ''){ //不输入
        shopnum = that.data.shoppr_num;
      };
      if(shopnum==0){
        wx.showLoading({
          title:'输入不能为0',
          duration:500
        });
        return;
      };
    };
    if (shopnum>9999.99){
      wx.showToast({
        title: '数量过大'
      })
      return;
    };
    var aux_obj={'is_aux':0,'pr_unit':this.data.tanunit_index,'sub_unit_num':1};
    if(that.data.shop_price_type==1 || that.data.shop_price_type==2){
      if(that.data.price_type==2){      //客户级别价
        if(this.data.tan_is_auxiliary==1){  //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid,shopnum, that.data.tszhcontent,nor_price_obj.true_price,nor_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        }else{     //非辅助单位
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_level_price,that.data.shop_level_b_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);  
        }
      }else if(that.data.price_type==1){ //客户历史价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid,shopnum, that.data.tszhcontent,nor_price_obj.true_price,nor_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num,'',nor_price_obj.true_price);
        }else{  //非辅助单位
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,0,0,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num,'',that.data.shop_price);
        }    
      }else if(that.data.price_type==4){  //客户固定价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid,shopnum, that.data.tszhcontent,nor_price_obj.true_price,nor_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        }else{  //非辅助单位
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_price,that.data.shop_b_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        };
      }else{   //客户询价
        if(this.data.tan_is_auxiliary==1){ //是辅助单位
          if(that.data.aux_pf_list.length==0){   //当接口还未获取到辅助单位列表时return
            return;
          };
          var aux_item=that.data.aux_pf_list[that.data.aux_pf_index];
          var nor_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
          aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid,shopnum, that.data.tszhcontent,nor_price_obj.true_price,nor_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        }else{  //非辅助单位
          app.myMethod.updateGood(that.data.supplier_id,this.data.b_supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,0,0,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        };
      }
    }else if(that.data.shop_price_type==3){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){ //模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && shopnum>0 && shopnum*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':that.data.tan_ku_price,'single_price':0};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(that.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=that.data.aux_kucun_list[that.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          app.myMethod.updateGood(that.data.supplier_id,this.data.group_id,that.data.shopid, shopnum, that.data.tszhcontent,kucun_price_obj.true_price,kucun_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        }else if(this.data.tan_shop_ku==0){  //非库存商品
          if(this.data.is_zi_cai==0 || that.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=that.data.assign_superlist[that.data.assign_super_index];
            var nor_price_obj={'true_price':that.data.shop_price,'single_price':0};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            app.myMethod.updateGood(that.data.supplier_id,assign_item.supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,nor_price_obj.true_price,nor_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num,assign_item.submit_price);
          }else if(this.data.is_zi_cai==1 && that.data.assign_super_index==-10010){  //自采
            app.myMethod.updateGood(-10010,-10010,that.data.shopid,shopnum,that.data.tszhcontent,that.data.shop_price,0,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
          };    
        }   
      }else if(this.data.good_dir_set==2 && this.data.purchase_type==2){  //模式6,7
        app.myMethod.updateGood(that.data.supplier_id,this.data.group_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_price,0,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
      }else{  //模式1,4及其他
        app.myMethod.updateGood(that.data.supplier_id,that.data.b_supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_price,0,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
      };   
    }else if(that.data.shop_price_type==4){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){  //模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && shopnum>0 && shopnum*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':that.data.tan_ku_price,'single_price':that.data.tan_ku_price};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(that.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=that.data.aux_kucun_list[that.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          app.myMethod.updateGood(that.data.supplier_id,this.data.group_id,that.data.shopid, shopnum, that.data.tszhcontent,kucun_price_obj.true_price,kucun_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        }else if(this.data.tan_shop_ku==0){  //非库存商品
          if(this.data.is_zi_cai==0 || that.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=that.data.assign_superlist[that.data.assign_super_index];
            var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            app.myMethod.updateGood(that.data.supplier_id,assign_item.supplier_id,that.data.shopid,shopnum,that.data.tszhcontent,nor_price_obj.true_price,nor_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num,assign_item.submit_price);
          }else if(this.data.is_zi_cai==1 && that.data.assign_super_index==-10010){   //自采
            app.myMethod.updateGood(-10010,-10010,that.data.shopid,shopnum,that.data.tszhcontent,that.data.shop_price,that.data.shop_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
          };    
        }       
      }else if(this.data.good_dir_set==2 && this.data.purchase_type==2){  //模式6,7
        app.myMethod.updateGood(that.data.supplier_id,this.data.group_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_price,that.data.shop_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
      }else{   //模式1,4及其他
        app.myMethod.updateGood(that.data.supplier_id,that.data.b_supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_price,that.data.shop_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
      };
    }else if(that.data.shop_price_type==5){
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){  //模式5
        if(this.data.tan_shop_ku==1){  //库存商品
          if(this.data.is_ctrl_stock==1 && shopnum>0 && shopnum*1>this.data.tan_ku_num*1){
            wx.showLoading({
              title:'库存不足',
              duration:500
            });
            return;
          };
          var kucun_price_obj={'true_price':that.data.tan_ku_price,'single_price':that.data.tan_ku_price};
          if(this.data.tan_is_auxiliary==1){  //是辅助单位
            if(that.data.aux_kucun_list.length==0){   //当接口还未获取到库存辅助单位列表时return
              return;
            };
            var aux_item=that.data.aux_kucun_list[that.data.aux_kucun_index];
            aux_obj={'is_aux':1,'pr_unit':aux_item.sub_unit,'sub_unit_num':aux_item.sub_unit_num};
            if(this.data.shop_is_same==0){  //是否开启采销同价
              kucun_price_obj={'true_price':aux_item.sale_price,'single_price':aux_item.sale_price};
            }else if(this.data.shop_is_same==1){
              kucun_price_obj={'true_price':aux_item.sub_price,'single_price':aux_item.sub_price};
            };
          };
          app.myMethod.updateGood(that.data.supplier_id,this.data.group_id,that.data.shopid, shopnum, that.data.tszhcontent,kucun_price_obj.true_price,kucun_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
        }else if(this.data.tan_shop_ku==0){  //非库存商品
          if(this.data.is_zi_cai==0 || that.data.assign_super_index>-1){  //非自采
            if(this.data.assign_superlist.length==0){   //当接口还未获取到供应商时return
              return;
            };
            var assign_item=that.data.assign_superlist[that.data.assign_super_index];
            var nor_price_obj={'true_price':assign_item.unit_price,'single_price':assign_item.unit_price};
            if(this.data.tan_is_auxiliary==1){  //是辅助单位
              aux_obj={'is_aux':1,'pr_unit':assign_item.sub_unit,'sub_unit_num':assign_item.sub_unit_num};
              nor_price_obj={'true_price':assign_item.sub_price,'single_price':assign_item.sub_price};
            };
            app.myMethod.updateGood(that.data.supplier_id,assign_item.supplier_id,that.data.shopid, shopnum,that.data.tszhcontent,nor_price_obj.true_price,nor_price_obj.single_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num,assign_item.submit_price);
          }else if(this.data.is_zi_cai==1 && that.data.assign_super_index==-10010){  //自采
            app.myMethod.updateGood(-10010,-10010,that.data.shopid, shopnum,that.data.tszhcontent,that.data.shop_sale_price,that.data.shop_sale_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
          };      
        }; 
      }else if(this.data.good_dir_set==2 && this.data.purchase_type==2){   //模式6,7
        app.myMethod.updateGood(that.data.supplier_id,this.data.group_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_sale_price,that.data.shop_sale_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
      }else{    //模式1,4及其他
        app.myMethod.updateGood(that.data.supplier_id,that.data.b_supplier_id,that.data.shopid, shopnum, that.data.tszhcontent,that.data.shop_sale_price,that.data.shop_sale_price,1,aux_obj.is_aux,aux_obj.pr_unit,aux_obj.sub_unit_num);
      };
    };
    if(this.data.isInput==true){
      util.hideBuyModal(this,"isInput");
    };
    wx.showLoading({
      title: '更新成功',
      duration: 500
    });
    this.updateGouWuChe(this.data.supplier_id, this.data.one_super);
    if(this.data.is_sao_code==1){
      this.saomiao_ck();
    };
    this.setData({
      isinput_col:false,
      tszhcontent: '',
      inputIndex: -1
    });  
  },
  // 选择菜品时的供应商和价格
  assign_super_fun:function(good_id,is_aux){
    var that = this;  
    app.AppHttp('api/Shopping/getGoodSupplierInfo', {
      order_id:0,
      client_id: app.globalData.loginInfo.id,
      good_id:good_id,
      is_aux:is_aux
    }, function (res) {
      console.log(JSON.stringify(res.data));   
      that.setData({
        is_move_gif:false,
        assign_superlist:res.data
      });
      if(that.data.is_zi_cai==1){  //开通自采
        that.zicai_pr_num(good_id);
      };
      if(that.data.isinput_col && (res.data.length==0 && that.data.is_zi_cai==1 || res.data.length==1 && that.data.is_zi_cai==0)){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };   
    });   
  },
  // 库存辅助单位接口
  aux_kucun_fun(good_id){
    var that = this; 
    app.AppHttp('api/Shopping/getStockAuxUnit', {
      order_id:0,
      good_id:good_id,
      client_id:app.globalData.loginInfo.id
    }, function (res) {
      console.log(JSON.stringify(res.data));  
      that.setData({
        is_move_gif:false,
        aux_kucun_list:res.data
      });
      if(that.data.isinput_col && res.data.length==1){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };
    });  
  },
  // 供应-批发端辅助单位接口
  aux_gy_pf_fun(good_id){
    var that = this; 
    app.AppHttp('api/Shopping/getSupplierGoodsAux', {
      order_id:0,
      client_id:app.globalData.loginInfo.id,
      good_id:good_id,
      supplier_id:that.data.supplier_id
    }, function (res) {
      console.log(JSON.stringify(res.data)); 
      let aux_pf_list=[];
      if(res.data.goodsAuxiliaryList.length>0){
        aux_pf_list = res.data.goodsAuxiliaryList.filter((c,i) => {
          if(c.is_order_unit==1){
            if(that.data.price_type==2){
              res.data.goodsAuxiliaryList[i].sub_price = res.data.goodsAuxiliaryList[i].level_sub_price;
              return true;
            }else{
              res.data.goodsAuxiliaryList[i].sub_price = res.data.goodsAuxiliaryList[i].his_sub_price;
              return true;
            };
          };
        });
      };
      that.setData({
        is_move_gif:false,
        aux_pf_list:aux_pf_list
      });
      if((that.data.price_type==1 || that.data.price_type==4) && aux_pf_list.length==0){
        that.setData({
          tan_is_auxiliary:0 //**有辅助单位，但还没有历史价，按无辅助单位处理
        })
        if(that.data.isinput_col){
          that.open_tan_info();
        }
      };
      if((that.data.isinput_col || that.data.is_open_type=='normal') && aux_pf_list.length==1){   //当收藏弹窗显示时，才有可能调用
        that.open_tan_info();
      };
    });  
  },
  // 自采在购物车中的的数量
  zicai_pr_num(good_id){
    var that = this; 
    app.AppHttp('api/Shopping/getShopSelfMining', {
      openid:app.globalData.openid,
      good_id:good_id
    }, function (res) {
      console.log(JSON.stringify(res.data));  
      if(res.data.data){
        that.setData({
          zi_tannum:res.data.data.pr_num,
          tannum:res.data.data.pr_num,
          shoppr_num:res.data.data.pr_num
        });
      }else{
        that.setData({
          zi_tannum:0,
          tannum:0,
          shoppr_num:0
        });
      }; 
    });
  },
  // 弹窗内容展示
  tancontent: function () {
    var that = this;
    console.log(that.data.shopid);
    app.AppHttp('api/Shopping/addCoolGoodsPage', {
      openid: app.globalData.openid,
      good_id: that.data.shopid,
      is_ku:that.data.is_ku
    }, function (res) {
      console.log(JSON.stringify(res.data));
      that.setData({
        taninfo: res.data,    //弹窗数据
        isinput_col: true,
        shopnum: '',
        shoppr_num: ''
      })
      if (res.data.specs_list.length == 1) {
        that.setData({
          ischoose_index: 0,  //当规格只有一个时为选中状态
          tszhcontent: res.data.specs_list[0].remark?res.data.specs_list[0].remark:'', 
          shoppr_num: res.data.specs_list[0].pr_num,  //选中的产品是否已添加
          shop_price_type:res.data.specs_list[0].price_type,  //选中的产品价格类型
          shop_price:res.data.specs_list[0].true_price, //选中的产品的价格
          shop_b_price:res.data.specs_list[0].single_price, //选中的产品B单价格
          shop_sale_price:res.data.specs_list[0].sale_price, //选中的产品销售价
          shop_level_price:res.data.specs_list[0].level_true_price,  //级别A价
          shop_level_b_price:res.data.specs_list[0].level_single_price,  //级别B价
          tan_ku_price:that.data.shop_is_same==0?res.data.specs_list[0].stock_sale_price:res.data.specs_list[0].stock_price_show,
          tan_ku_num:res.data.specs_list[0].stock_num_show,
          is_tan_ku_price:res.data.specs_list[0].is_show_stock_price,
          is_tan_ku_num:res.data.specs_list[0].is_show_stock_num
        });
      }
    });
  },
  // 选定条件后打开底部弹窗
  open_tan_info:function(e){
    if(this.data.ischoose_index>=0){
      util.showBuyModal(this);
      var taninfo = this.data.taninfo;
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1){   //模式5
        if(this.data.tan_shop_ku==1){    //库存商品
          if(this.data.tan_is_auxiliary==0){   //未开启辅助单位
            this.setData({
              assign_super_index:-1,
              aux_kucun_index:-1,
              aux_kucun_list:[],
            });
          }else if(this.data.tan_is_auxiliary==1){  //开启了辅助单位
            if(this.data.aux_kucun_list.length==1){  //仅有一条辅助单位
              this.setData({
                assign_super_index:-1,
                aux_kucun_index:0,
                shoppr_num: this.data.aux_kucun_list[0].pr_num,
              });
            }else{
              this.setData({
                assign_super_index:-1,
                aux_kucun_index:e.currentTarget.dataset.index,
                shoppr_num: this.data.aux_kucun_list[e.currentTarget.dataset.index].pr_num
              });
            };     
          };
        }else if(this.data.tan_shop_ku==0){  //非库存商品
          if(this.data.assign_superlist.length==0){  //自采
            this.setData({
              aux_kucun_index:-1,
              aux_kucun_list:[],
              assign_super_index:-10010
            });
            this.zicai_pr_num(taninfo.specs_list[this.data.ischoose_index].id);
          }else if(this.data.assign_superlist.length==1 && this.data.is_zi_cai==0){  //仅一个供应商+未开自采
            this.setData({
              aux_kucun_index:-1,
              aux_kucun_list:[],
              assign_super_index:0,
              shoppr_num:this.data.assign_superlist[0].pr_num
            });
          }else{
            if(e.currentTarget.dataset.index!=-10010){
              this.setData({
                shoppr_num:this.data.assign_superlist[e.currentTarget.dataset.index].pr_num
              });
            }else if(e.currentTarget.dataset.index==-10010){
              this.zicai_pr_num(taninfo.specs_list[this.data.ischoose_index].id);
            };
            this.setData({
              aux_kucun_index:-1,
              aux_kucun_list:[],
              assign_super_index:e.currentTarget.dataset.index,
            });   
          }
        }  
      }else{  //其他
        if(taninfo.specs_list[this.data.ischoose_index].price_type<=2){  //供应-批发
          if(this.data.tan_is_auxiliary==0){
            this.setData({
              aux_pf_index:-1,
              aux_pf_list:[],
            });
          }else if(this.data.tan_is_auxiliary==1){
            if(this.data.aux_pf_list.length==1){   //仅一条辅助单位
              this.setData({
                aux_pf_index:0,
                shoppr_num:this.data.aux_pf_list[0].pr_num 
              })
            }else{
              this.setData({
                aux_pf_index:e.currentTarget.dataset.index,
                shoppr_num:this.data.aux_pf_list[e.currentTarget.dataset.index].pr_num 
              })
            }
          }
        }   
      }
      let tan_true_price='';
      let tan_single_price='';
      if(this.data.price_type==2){
        tan_true_price=taninfo.specs_list[this.data.ischoose_index].level_true_price;
        tan_single_price=taninfo.specs_list[this.data.ischoose_index].level_single_price;
      }else{
        tan_true_price=taninfo.specs_list[this.data.ischoose_index].true_price;
        tan_single_price=taninfo.specs_list[this.data.ischoose_index].single_price;
      }
      this.setData({
        content:'',
        isInput: true,
        is_addshop:true,   //添加商品
        tanimg: taninfo.goodInfo.cover,
        tanname: taninfo.goodInfo.trade_name,
        tanalias:taninfo.goodInfo.trade_alias,
        tanspecs:taninfo.specs_list[this.data.ischoose_index].specs,
        tan_fixed_show:taninfo.specs_list[this.data.ischoose_index].is_show_price,  //是否显示固定价
        tanunit_index:taninfo.specs_list[this.data.ischoose_index].help_unit>0?taninfo.specs_list[this.data.ischoose_index].help_unit:taninfo.specs_list[this.data.ischoose_index].unit,
        tanunit:taninfo.specs_list[this.data.ischoose_index].help_unit>0?taninfo.specs_list[this.data.ischoose_index].help_unit_name:taninfo.specs_list[this.data.ischoose_index].unit_name,
        tanunit_init: taninfo.specs_list[this.data.ischoose_index].unit_name,
        tan_price_type: taninfo.specs_list[this.data.ischoose_index].price_type,
        tan_true_price:tan_true_price,
        tan_single_price:tan_single_price, 
      });
    }else{
      wx.showLoading({
        title:'请选择规格',
        duration:500
      })
    };
  },
  col_setValue:function(){
    this.setData({
      shopnum: this.data.content
    });
    console.log(this.data.shopnum);
  },
  // 取消关闭添加商品弹窗
  cancel_col: function () {
    this.setData({
      isinput_col: false
    });
  },
  // 选择商品规格
  chose_specs: function (e) {
    var index = e.currentTarget.dataset.index;
    var item=this.data.taninfo.specs_list[index];
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && this.data.tan_shop_ku==0 && item.tan_is_auxiliary==1 && index>0){
      wx.showModal({
        title: '提示',
        content:'当非库存商品开启辅助单位功能后，商品其他规格则被锁定',
        showCancel: false,
        confirmText:"我知道了",
        success: function (res) {}
      });   
      return;
    }else if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 &&this.data.tan_shop_ku==0 && (item.is_auxiliary==1 && item.is_aux_up*1+this.data.is_zi_cai*1==0 || item.is_auxiliary==0 && item.is_up*1+this.data.is_zi_cai*1==0)){
      wx.showModal({
        title: '提示',
        content:'当前规格无可选供应商',
        showCancel: false,
        confirmText:"我知道了",
        success: function (res) {}
      });
      return;
    };
    var is_zi_cai=this.data.is_zi_cai;
    this.setData({   
      ischoose_index: index,
      tszhcontent:item.remark?item.remark:'',
      shopid:item.id,  //选中的产品id
      shoppr_num:item.pr_num,  //选中的产品是否已添加
      shop_price_type:item.price_type,  //选中的产品价格类型
      shop_price:item.true_price, //选中的产品的价格
      shop_b_price:item.single_price,  //选中的产品B单价格
      shop_sale_price:item.sale_price,  //选中的产品销售价
      shop_level_price:item.level_true_price,  //级别A价
      shop_level_b_price:item.level_single_price,  //级别B价
      shop_is_same:item.is_same,  //是否开启采销同价
      tan_ku_price:item.is_same==0?item.stock_sale_price:item.stock_price_show,
      tan_ku_num:item.stock_num_show,
      is_tan_ku_price:item.is_show_stock_price,
      is_tan_ku_num:item.is_show_stock_num,
      assign_superlist:[],
      assign_super_index:-1,
      aux_pf_index:-1,
      aux_pf_list:[],
    });
    if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && this.data.tan_shop_ku==0){   //模式5+非库存商品  
      this.setData({
        is_move_gif:true
      });
      this.assign_super_fun(item.id,this.data.tan_is_auxiliary);     
    }else{
      this.setData({
        is_move_gif:this.data.tan_is_auxiliary==1?true:false
      });
      if(this.data.good_dir_set==2 && this.data.client_is_store==0 && this.data.purchase_type==1 && this.data.tan_shop_ku==1 && this.data.tan_is_auxiliary==1){  //库存+辅助
        this.aux_kucun_fun(item.id);   
      }else{
        if(item.price_type<=2){  //供应-批发
          if(this.data.tan_is_auxiliary==1){ //是辅助单位
            this.setData({
              is_move_gif:true
            });
            this.aux_gy_pf_fun(item.id)
          }else if(this.data.tan_is_auxiliary==0){  //非辅助单位
            this.setData({
              is_move_gif:false
            });
            this.open_tan_info();
          } 
        }else{
          this.open_tan_info();
        }
      };
    }
  },
  // 筛选分类
  open_screen_class(){
    if(this.data.is_screen){
      this.close_screen();
    }else{
      let { ...chose_one_class } = this.data.chose_one_class;
      if(!this.data.is_screen){
        util.showBuyModal(this,-1);
      };
      this.setData({
        is_screen:true,
        win_chose_one_class:chose_one_class
      });
    }
  },
  // 关闭筛选
  close_screen(){
    this.setData({
      is_screen:false,
    });
  },
  // 选择分类
  chose_fenlei(e){
    let one_class=e.currentTarget.dataset.one_class;
    let win_chose_one_class = this.data.win_chose_one_class;
    if(win_chose_one_class[one_class]){
      delete win_chose_one_class[one_class];
    }else{
      win_chose_one_class[one_class] = true;
    };
    this.setData({
      win_chose_one_class:win_chose_one_class
    });
  },
  // 重置分类
  resetClass(){
    this.setData({
      chose_one_class:{},
      win_chose_one_class:{},
      is_screen:false,
      count:0, 
      isChooseAll: false,    //全选
      isclasscheck:{}, //类选
      is_checked: {},//单选
    });
    this.updateGouWuChe(this.data.supplier_id,this.data.one_super);
  },
  // 筛选分类
  searClass(){
    let { ...win_chose_one_class } = this.data.win_chose_one_class;
    this.setData({
      chose_one_class:win_chose_one_class,
      is_screen:false,
      count:0, 
      isChooseAll: false,    //全选
      isclasscheck:{}, //类选
      is_checked: {},//单选
    });
    this.updateGouWuChe(this.data.supplier_id,this.data.one_super);
  },
  // 键盘按键
  keyTap(e) {
    util.keyfunction(e,this,3);
    if(this.data.is_addshop==true){
      this.col_setValue();
    }else{
      this.setValue();
    };
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let PAGE_H = wx.getSystemInfoSync().windowHeight;
    let PAGE_W = wx.getSystemInfoSync().windowWidth;
    this.setData({
      RPX_SEAR:PAGE_H-130/750 * PAGE_W,
      left_open: 0,    //默认打开第一个供应商索引
      count: 0,    //删除后已选商品数量归零
      isChooseAll: false,    //全选
      isclasscheck:{}, //类选
      is_checked: {},//单选
    });
    this.updateGouWuChe(this.data.supplier_id,this.data.one_super);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.updateGouWuChe(this.data.supplier_id,this.data.one_super);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})